<?php

function upImg($model, $key, $destPath, $label){
    if(!empty($model->$key) && empty($_FILES[$key])){
        return;
    }

    //有图片继续上传
    if(empty($_FILES[$key]['name'])) {
        $model->addError($key, '请上传'.$label);
    }else{
        try {
            @unlink(Yii::app()->getBasePath().'/..'.$model->$key);
            $model->$key='/images/user/'.Yii::app()->user->id.'/'.JUserModel::upload($_FILES[$key], $destPath, $label);
        } catch (Exception $e) {
            $model->addError($key, $e->getMessage());
        }
    }
}

$user=new JUserModel;
$user->user_type=$user_type;
$user->user_title=$user_title;

foreach($user->attributes as $key => $val){
    $user->$key=isset($user_info->$key) ? $user_info->$key : (isset($user_info->p->$key) ? $user_info->p->$key : '');
}
$user->email=$user_info->email;
$user->username=$user_info->username;

//$user->attributes=$user_info->attributes;
//$user->attributes=$user_info->p->attributes;


$info_status=ProfilesStatus::model()->findByPk($user_info->id);
if(empty($info_status)){
    $info_status=new ProfilesStatus();
    $info_status->user_id=$user_info->id;
}

$check_item=array(
    '',
    '佩特里网',
    '信息保密',
    '软件工程',
    '开放系统',
    '互联网',
    '普适计算',
    '数据库',
    '协同计算',
    '游戏动漫',
    '体系结构',
    '服务计算',
    '容错计算',
    '系统软件',
    '多媒体技术',
    '办公自动化',
    '中文信息技术',
    '高性能计算',
    '传感器网络',
    '信息存储技术',
    '理论计算机科学',
    '计算机应用计算机安全',
    '计算机工程与工艺',
    '计算机教育教学',
    '金融信息处理',
    '人工智能与模式识别',
    '多值逻辑与模糊逻辑',
    '工业控制计算机',
    '抗恶劣环境计算机',
    '计算机辅助设计与图形学',
    '网络与数据通信',
    '虚拟现实与可视化技术'
);


if(isset($_POST['JUserModel']))
{
    $user->attributes=$_POST['JUserModel'];
    foreach($user->attributes as $key => $val){
        $user->$key=(isset($_POST['JUserModel'][$key])&& !empty($_POST['JUserModel'][$key])) ? $_POST['JUserModel'][$key] : $user->$key;
    }

    $destPath = Yii::app()->getBasePath().'/../images/user/'.Yii::app()->user->id.'/';

    upImg($user_info->p, 'pphoto', $destPath, '个人照片');
    upImg($user_info->p, 'c_introduce_photo', $destPath, '组织机构形象照');
    upImg($user_info->p, 'c_id_photo', $destPath, '组织机构形象照');

    $user->pphoto=$user_info->p->pphoto;
    $user->c_introduce_photo=$user_info->p->c_introduce_photo;
    $user->c_id_photo=$user_info->p->c_id_photo;

    if(isset($_POST['open'])){
        $info_status->attributes=$_POST['open'];
        $info_status->save();
    }

    if(isset($_POST['JUserModel']['n_activity']) && is_array($_POST['JUserModel']['n_activity'])){
        if(count($_POST['JUserModel']['n_activity'])>3){
              $user->n_activity=array_slice($_POST['JUserModel']['n_activity'], 0, 3);
//            $user->addError('n_activity','希望参加的专项活动:超过3个栏目限制');
        }

        if(count($_POST['JUserModel']['n_activity'])>0){
            $check_name=array();
            foreach($_POST['JUserModel']['n_activity'] as $key_i => $val_i){
                $check_name[]=$check_item[$key_i];
                if(count($check_name)==3)
                    break;
            }
            $user->n_activity=implode(',',$check_name);
        }
    }


    if($user->validate())
    {
        $user_info->email= $user->email;
//        $user_info->save();

        $user_info->p->attributes=$user->attributes;
        foreach($user_info->p->attributes as $key => $val){
            $user_info->p->$key=$user->$key;
        }
        $user_info->p->open=0;
        $user_info->p->user_id=$user_info->id;
        if($user_info->p->save()){
//            $user_info->status=0;
            if($user_info->validate()){

                $user_info->save();

                $massage=new Message();
                $massage->pid=0;
                $massage->post_uid=Yii::app()->user->id;
                $massage->to_uid=1;
                $massage->content='用户<a href="/index.php?r=users/index&type=1&id='.$user_info->id.'">'.$user_info->p->nickname.'</a>更新了个人信息，等待审核.';
                $massage->status=0;
                $massage->c_delete=1;
                $massage->open=1;
                $massage->title='申请审核用户:'.$user_info->p->nickname;
                $massage->view=1;
                $massage->time=date("Y-m-d H:i:s");
                $massage->save();
            }
        }
    }
}

$n_activity_key=explode(',', $user->n_activity);
if(count($n_activity_key)>0){
    $n_activity_val=array();
    foreach($n_activity_key as $kk){
        foreach($check_item as $k_n => $v_n){
              if($kk==$v_n){
                   $n_activity_val[]=$k_n;
              }
        }
    }
}
$user->n_activity= $n_activity_val;
//pd($user->n_activity);

$user->password='';
$user->verifyPassword='';
$user->user_type=$user_type;
$user->user_title=$user_title;


$this->renderPartial('comform',array(
    'table_width'=>'925',
    'title'=>'修改注册信息',
    'info_status'=>$info_status,
    'model'=>$user,
    'user_type'=>$user_type,
    'user_title'=>$user_title,
));



